package com.half_q.equipment_management_halfq.controller;

import com.half_q.equipment_management_halfq.service.impl.UserServiceImpl;
import domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 简介说明: 用户控制层
 *
 * @author: Half_Q_0115
 * @date: 2025/01/07 20:57:17
 * @version: 1.0
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/addUser")
    public int addUser(User user){
        return userService.addUser(user);
    }

    @RequestMapping("/deleteUserById")
    public int deleteUserById(String id){
        return userService.deleteUserById(id);
    }

    @PostMapping("/checkUser")
    @ResponseBody
    public boolean checkUser(@RequestParam String username, @RequestParam String password, HttpSession session){
        System.out.println("checkUser" + username + " " + password);
        if (username == null || password == null) {
            return false;
        }

        boolean isValidUser = userService.checkUser(username, password);
        if (isValidUser) {
            session.setAttribute("id", username);
            session.setAttribute("identity", userService.getIdentityById(username));
            session.setAttribute("name", userService.getNameById(username));
            session.setAttribute("college_id", userService.getCollegeIdById(username));
            return true;
        }
        return false;
    }

    @RequestMapping("/getIdentityById")
    public int getIdentityById(String id){
        return userService.getIdentityById(id);
    }

    @RequestMapping("/getNameById")
    public String getNameById(String id){
        return userService.getNameById(id);
    }

    @RequestMapping("/checkPhone")
    public boolean checkPhone(String phone_number){
        return userService.checkPhone(phone_number);
    }

    @RequestMapping("/getIdByPhone")
    public String getIdByPhone(String phone_number){
        return userService.getIdByPhone(phone_number);
    }

    @RequestMapping("/getIdentityByPhone")
    public int getIdentityByPhone(String phone_number) {
        return userService.getIdentityByPhone(phone_number);
    }

    @RequestMapping("/getNameByPhone")
    public String getNameByPhone(String phone_number) {
        return userService.getNameByPhone(phone_number);
    }

    @RequestMapping("/getCollegeIdById")
    public int getCollegeIdById(String id) {
        return userService.getCollegeIdById(id);
    }

    @RequestMapping("/getCollegeIdByPhone")
    public int getCollegeIdByPhone(String phone_number) {
        return userService.getCollegeIdByPhone(phone_number);
    }

    @RequestMapping("/findAllUser")
    @ResponseBody
    public List<User> findAllUser() {
        return userService.findAllUser();
    }

    @RequestMapping("/searchUser")
    public List<User> searchUsers(String id, String name, String gender, String college_id, String identity, String phone_number) {
        return userService.searchUsers(id, name, gender, college_id, identity, phone_number);
    }

    @RequestMapping("/findUserById")
    public User findUserById(String id){
        return userService.findUserById(id);
    }

    @RequestMapping("/modifyUser")
    public int modifyUser(User user) {
        return userService.modifyUser(user);
    }
}
